<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @author Garry
 * @date 01/5/2011
 * @desc Display classes carry info to the views
 */
class Display {
	public $base = "";
	/**
	 *
	 * this is the parent page element
	 * @var Display
	 */
	public $wrapper;
	/**
	 *
	 * Needed to be class wide
	 * @var array
	 */
	protected static $scripts = array();
	/**
	 * Needed to be class wide
	 * @var array
	 */
	protected static $styles = array();
	/**
	 *
	 * Needed to be class wide
	 * @var unknown_type
	 */
	protected static $initialisedJavaScript = array();
	/***
	 * The dom document
	 */
	protected $dom;
	/**
	 *
	 * This is the output values of the display.
	 * It could be a simple attribute, a component or html
	 * @var unknown_type
	 */
	public $output;
	/**
	 * 
	 * Construct the display
	 */
	public function __construct( $base = NULL){
		if( isset($base) ){
			$this->base = $base;
		}
	}
	/**
	 *
	 * adds a child display to this composite
	 * @param Display $display
	 */
	public function addChild( Display  $display,$token = "" ){
		// might not be defined
		if( empty($display->output) ){
			$display->output = new stdClass();
		}
		foreach( $display->output as $key => $val ){
			if( !empty( $this->$key ) ){
				if( is_array($this->$key) ){
					array_push($this->$key, $val);
				}
				else {
					$temp = $this->$key;
					$this->$key = array();
					array_push($this->$key, $temp);
					array_push($this->$key, $val);
				}
			}
			else {
				$this->$key =$val;
			}
		}
		if( !empty($token)){
			$this->$token = $display->output->content;
		}

	}

	public function addJSInit($lines){
		if( !in_array($lines, Display::$initialisedJavaScript)){
					Display::$initialisedJavaScript[]=$lines;
				}
	}
	/**
	 *
	 * gets a property if it exists
	 * @param unknown_type $prop
	 */
	public function __get( $prop ){
		if( empty( $this->output ) ){
			$this->output = new stdClass();
		}
		return $this->output->$prop;
	}

	/**
	 *
	 * sets the property with a value
	 * @param unknown_type $prop
	 * @param unknown_type $val
	 */
	public function __set( $prop, $val){
		$this->checkContent( $val );
		if( empty( $this->output) ){
			$this->output = new stdClass();
		}
		if( ($prop=="scripts"||$prop=="styles") ){
			if($prop=="scripts"){ 
				if( !in_array($val, Display::$scripts)){
					Display::$scripts[]=$val;
				}
			}
			if($prop=="styles"){
				if( !in_array($val, Display::$styles)){
					Display::$styles[]=$val;
				}
			}
			return;
		}
		if( !empty( $this->output->$prop )&& $prop != "content"  ){
			if( is_array($this->output->$prop) ){
				// check to see if this is not already here if script or stylesheet
				array_push($this->output->$prop, $val);
			}
			else {
				$temp = $this->output->$prop;
				$this->output->$prop = array();
				array_push($this->output->$prop, $temp);
				array_push($this->output->$prop, $val);
			}
		}
		else {
			$this->output->$prop = $val;
		}
	}
	/**
	 * Check to see if we have any content to parse
	 * Enter description here ...
	 * @param unknown_type $str
	 */
	protected function checkContent( &$incoming ){
		if( is_string( $incoming ) && strpos( $incoming,"div")>0 ){
			 $incoming = $this->GetGeneratedContent( $incoming );
		}
	}
	
	protected function PropertyExists( $aProps,$prop,$val ){
		if( is_array( $aProps ) ){
				
		}
	}
	/**
	 *
	 * Assembles all child displays plus own view
	 * 
	 */
	public function finalize(){
		if( !empty($this->base)){
			$this->content = trim(str_replace('<?xml version="1.0"?>','',get_instance()->load->view($this->base,$this->output,TRUE)));
		}		

	}
	
	/**
	 * 
	 * Process the template to look for ContentManaged Section
	 */
	protected function GetGeneratedContent( $content ){
		// load up the html
		if(strlen($content)<=0)return;
		$converted = mb_convert_encoding($content, 'UTF-8', 'KOI8-R');
		$this->dom = DOMDocument::loadHTML( $converted );
		$divs = $this->dom->getElementsByTagName('div');
		$found = false;
		foreach ( $divs as $div ) {
			$id = $div->attributes->getNamedItem("id");
			if( $id instanceof DOMAttr ){
				if( strpos( $id->value,"?" ) === 0 ){
					$this->DOMinnerHTML( $this->ParseContent( $div,$id->value ) );
					$found = true;
				}
			}
		}
		if(!$found)return $content;
		$ret = substr($this->dom->saveHTML(),strlen("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">
<html><body>")-1);
		$ret = substr($ret,0,strlen($ret)-strlen("</body></html>")-1);
		return $ret;
	}
	
	/**
	 * 
	 * Grabs the inner html
	 * @param unknown_type $element
	 */
	protected function DOMinnerHTML( DomNode $element) 
	{ 
    $innerHTML = ""; 
    $children = $element->childNodes; 
    foreach ($children as $child) 
    { 
        $tmp_dom = new DOMDocument(); 
        $tmp_dom->appendChild($tmp_dom->importNode($child, true)); 
        $innerHTML.=trim($tmp_dom->saveHTML()); 
    } 
    return $innerHTML; 
	} 
	/***
	 * Get the content generated by the ContentManager
	 */
	protected function ParseContent( DOMNode &$node, $contentID ){
		$contentID = (string)substr($contentID, 1,strlen($contentID)-2 );
		$contentManager = new ContentManager( $contentID );
		$fragment = $this->dom->createDocumentFragment();
		$fragment->appendXML( $contentManager->display->$contentID );
		$node->appendChild( $fragment );
		$node->attributes->getNamedItem("id")->value = $contentID;
		return $node;
	}
	/**
	 * Returns any generated content management ids
	 * @return array
	 */
	public function GetContentManagedSections(){
		print_r($this->base);
	}
}
?>